package com.cloud.wms.ui.inside.revert;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.eventBus.Event;
import com.cloud.core.eventBus.EventBusUtil;
import com.cloud.core.utils.NumberUtils;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.inside.contract.RevertContact;
import com.cloud.wms.ui.inside.entity.RevertBatchEntity;
import com.cloud.wms.ui.inside.presenter.RevertPresenter;
import com.cloud.wms.ui.main.entity.CommodityEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-03-19 10:17
 * @Description:
 */
public class RevertInventoryActivity extends BaseBarListActivity<CommodityEntity, RevertPresenter> implements RevertContact.View {
    @BindView(R.id.et_commodity_amount)
    EditText etCommodityAmount;
    @BindView(R.id.btn_confirm)
    TextView btnConfirm;
    private CommodityEntity commodityEntity;
    private int returnType;

    @Override
    protected String barTitle() {
        return "还货清点";
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_inside_revert_inventory;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        rvList.requestFocus();
        btnConfirm.setEnabled(true);
        etCommodityAmount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int amount = NumberUtils.getIntegerValue(s.toString());
                if (amount > 0) {
                    btnConfirm.setEnabled(true);
                } else {
                    btnConfirm.setEnabled(false);
                }
            }
        });

        commodityEntity = (CommodityEntity) getIntent().getParcelableExtra(IntentKey.INFO_KEY);
        returnType = getIntent().getIntExtra(IntentKey.TYPE_KEY, 1);

        mPresenter.loadPosCommodity(commodityEntity.getCommodityId(), commodityEntity.getPosCode(), returnType);
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, CommodityEntity item) {
        helper.setText(R.id.tv_commodity_code, item.getBarCode())
                .setText(R.id.tv_commodity_name, item.getCommodityName())
                .setText(R.id.tv_commodity_amount, item.getAmount() + "");
    }

    @Override
    protected int getResId() {
        return R.layout.activity_inside_revert_inventory;
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void showEmpty() {
        mAdapter.setEmptyView(getEmptyDataView());
    }

    @Override
    public void showError() {
        mAdapter.setEmptyView(getErrorView());
    }

    protected View getEmptyDataView() {
        View notDataView = getLayoutInflater().inflate(com.cloud.core.R.layout.layout_empty, rvList, false);
        notDataView.setOnClickListener(v -> {
            mPresenter.loadPosCommodity(commodityEntity.getCommodityId(), commodityEntity.getPosCode(), returnType);
        });
        return notDataView;
    }

    protected View getErrorView() {
        View errorView = getLayoutInflater().inflate(com.cloud.core.R.layout.layout_error, rvList, false);
        errorView.setOnClickListener(v -> {
            mPresenter.loadPosCommodity(commodityEntity.getCommodityId(), commodityEntity.getPosCode(), returnType);
        });
        return errorView;
    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case RevertPresenter.GET_POS_COMMODITY:
                mAdapter.setNewInstance(((ArrayList<CommodityEntity>) message.obj));
                break;
            case RevertPresenter.SUBMIT_REVERT_AMOUNT:
                success("还货成功！");
                EventBusUtil.sendEvent(new Event<>(EventConfig.CLOSE_REFRESH));
                this.finish();
                break;
        }
    }

    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        int amount = NumberUtils.getIntegerValue(etCommodityAmount.getText().toString());
        if (amount < 0) {
            info("请输入大于0的数量");
            return;
        }
        List<RevertBatchEntity> entityList = new ArrayList<>();
        RevertBatchEntity entity = new RevertBatchEntity();
        entity.setAmount(amount);
        entityList.add(entity);
        mPresenter.confirmUpShelf(commodityEntity.getCommodityId(), String.valueOf(commodityEntity.getPosID()), amount, returnType, entityList);
    }

    public static void start(Activity activity, CommodityEntity commodityBean, int type) {
        Intent intent = new Intent(activity, RevertInventoryActivity.class);
        intent.putExtra(IntentKey.INFO_KEY, commodityBean);
        intent.putExtra(IntentKey.TYPE_KEY, type);
        activity.startActivity(intent);
    }
}
